import time as lib_time

from django.utils.translation import gettext, gettext_lazy as _
from rest_framework import serializers
from rest_framework.exceptions import ValidationError


class MeetingReservationCreateSerializer(serializers.Serializer):
    subject = serializers.CharField(label=_('会议主题'), max_length=255, required=True)
    meeting_code = serializers.CharField(label=_('会议号'), required=True, help_text=_('腾讯会议号'))
    start_time = serializers.IntegerField(
        label=_('开始时间'), required=True, help_text=_('预定开始时间'),
    )
    end_time = serializers.IntegerField(
        label=_('结束时间'), required=True, help_text=_('预定结束时间'),
    )
    # people_num = serializers.IntegerField(label=_('参会人数'), required=True)
    room_type = serializers.IntegerField(
        label=_('会议室规格'), required=True, help_text=_('[50，100, 300，500, 1000, 2000]'))

    def validate(self, attrs):
        data = super().validate(attrs)
        meeting_code = data['meeting_code']
        start_time = data['start_time']
        end_time = data['end_time']
        room_type = data['room_type']
        # people_num = data['people_num']

        if '-' in meeting_code or ' ' in meeting_code:
            raise ValidationError(detail={'meeting_code': gettext('会议号不能包含连接符“-”和空格')})

        now_ts = int(lib_time.time())
        if start_time <= now_ts:
            raise ValidationError(detail={'start_time': gettext('预定开始时间必须大于当前时间')})

        if start_time >= end_time:
            raise ValidationError(detail={'start_time': gettext('预定结束时间必须大于开始时间')})

        if (end_time - start_time) < 60 * 30:
            raise ValidationError(detail={'start_time': gettext('预定时长不能小于30分钟')})

        if (end_time - start_time) > 3600 * 24:
            raise ValidationError(detail={'end_time': gettext('预定时长不能超过24小时')})

        if room_type not in [50, 100, 300, 500, 1000, 2000]:
            raise ValidationError(detail={'room_type': gettext('会议室规格无效，可选值[50，100，300，500，1000，2000]')})

        # if not (2 <= people_num <= 2000):
        #     raise ValidationError(detail={'people_num': gettext('参会人数无效，可选值为2至2000')})
        #
        # if people_num > room_type:
        #     raise ValidationError(detail={'room_type': gettext('会议室规格不满足参会人数要求')})

        return data


class BaseMeetingReservationSerializer(serializers.Serializer):
    """ 腾讯会议预定记录"""

    id = serializers.CharField(label='Id', read_only=True)
    subject = serializers.CharField(label=_('会议主题'), max_length=512)
    start_time = serializers.IntegerField(label=_('预定开始时间'), help_text=_('用户预定的开始时间'))
    end_time = serializers.IntegerField(label=_('预定结束时间'), help_text=_('用户预定的结束时间'))
    room_reserve_start = serializers.IntegerField(label=_('会议室预留开始时间'))
    room_reserve_end = serializers.IntegerField(
        label=_('会议室预留结束时间'),
        help_text=_('会议室资源预留的最终时间，会议室资源释放必须在此时间之前和用户预定结束时间之后'))
    people_num = serializers.IntegerField(label=_('会议人数'))
    room_type = serializers.IntegerField(label=_('会议室类型'))
    status = serializers.CharField(label=_('预定状态'))
    creation_time = serializers.DateTimeField(label=_('创建时间'))
    update_time = serializers.DateTimeField(label=_('修改时间'))
    user = serializers.SerializerMethodField(label=_('预定用户'), method_name='get_user')

    # 腾讯会议的信息
    tx_start_time = serializers.IntegerField(label=_('腾讯会议开始时'))
    tx_end_time = serializers.IntegerField(label=_('腾讯会议结束时'))
    tx_meeting_id = serializers.CharField(label=_('腾讯会议id'), max_length=32)
    tx_meeting_userid = serializers.CharField(label=_('腾讯会议创建者'), max_length=128)
    tx_user_nick_name = serializers.CharField(label=_('腾讯会议创建者昵称'), max_length=128)
    tx_meeting_code = serializers.CharField(label=_('腾讯会议号'), max_length=16)
    tx_join_url = serializers.CharField(label=_('腾讯会议加入链接'), max_length=254)

    # 虚拟会议室资源预留
    room_reserve_status = serializers.CharField(
        label=_('会议室预留状态'), help_text=_('去腾讯会议请求为用户预留虚拟会议室资源的状态')
    )
    room_reserve_desc = serializers.CharField(
        label=_('会议室预留状态描述'), max_length=254,
        help_text=_('请求腾讯会议预留和释放虚拟会议室资源的结果描述，可以记录失败信息'))

    # 会前通知
    advance_notice_seconds = serializers.IntegerField(
        label=_('发送会议提醒通知提前秒数'), help_text='用户设置的会前通知时间，会前提前多少秒给用户发送会议提醒通知')

    @staticmethod
    def get_user(obj):
        if obj.user:
            return {'id': obj.user.id, 'username': obj.user.username, 'full_name': obj.user.get_full_name()}

        return None


class MeetingReservationSerializer(BaseMeetingReservationSerializer):
    """ 腾讯会议预定记录"""
    email_notice_id = serializers.CharField(label=_('邮件通知记录id'))


class MeetingNoticeSerializer(serializers.Serializer):
    """会议提醒通知"""
    id = serializers.CharField(label='Id', read_only=True)
    notice_type = serializers.CharField(label=_('通知类型'))
    subject = serializers.CharField(label=_('主题'), max_length=254)
    content = serializers.CharField(label=_('提醒内容'), default='')
    sender = serializers.CharField(label=_('发送者'), max_length=254,)
    receiver = serializers.CharField(label=_('接收者'), max_length=254)
    status = serializers.CharField(label=_('发送状态'))
    status_desc = serializers.CharField(max_length=254, label=_('状态描述'))
    creation_time = serializers.DateTimeField(label=_('创建时间'))
    update_time = serializers.DateTimeField(label=_('修改时间'))


class MeetingEmailSerializer(serializers.Serializer):
    """会议邮件提醒通知"""
    id = serializers.CharField(label='Id', read_only=True)
    subject = serializers.CharField(max_length=255, label=_('标题'))
    sender = serializers.CharField(label=_('发送者'), default='')
    receiver = serializers.CharField(label=_('接收者'), max_length=254)
    message = serializers.CharField(label=_('邮件内容'))
    is_html = serializers.BooleanField(label='html格式信息', default=False)
    status = serializers.CharField(label=_('发送状态'))
    status_desc = serializers.CharField(label=_('状态描述'))
    send_time = serializers.DateTimeField(label=_('创建时间'))
    success_time = serializers.DateTimeField(label=_('成功发送时间'))


class MeetingReservationDetailSerializer(BaseMeetingReservationSerializer):
    """ 腾讯会议预定记录"""
    email_notice = MeetingEmailSerializer(label=_('邮件通知记录'), allow_null=True)


class MeetingRoomConfigSerializer(serializers.Serializer):
    id = serializers.CharField(label='Id', read_only=True)
    creation_time = serializers.DateTimeField(label=_('创建时间'), read_only=True)
    update_time = serializers.DateTimeField(label=_('更新时间'), read_only=True)

    type_50_num = serializers.IntegerField(label=_('50方数'), min_value=0, max_value=1000, default=0)
    type_50_visible = serializers.BooleanField(label=_('50方可见'), allow_null=True, default=None)
    type_100_num = serializers.IntegerField(label=_('100方数'), min_value=0, max_value=1000, default=0)
    type_100_visible = serializers.BooleanField(label=_('100方可见'), allow_null=True, default=None)
    type_300_num = serializers.IntegerField(label=_('300方数'), min_value=0, max_value=1000, default=0)
    type_300_visible = serializers.BooleanField(label=_('300方可见'), allow_null=True, default=None)
    type_500_num = serializers.IntegerField(label=_('500方数'), min_value=0, max_value=1000, default=0)
    type_500_visible = serializers.BooleanField(label=_('500方可见'), allow_null=True, default=None)
    type_1000_num = serializers.IntegerField(label=_('1000方数'), min_value=0, max_value=1000, default=0)
    type_1000_visible = serializers.BooleanField(label=_('1000方可见'), allow_null=True, default=None)


class UserSerializer(serializers.Serializer):
    """
    全局用户
    """
    id = serializers.CharField(label=_('ID'), read_only=True)
    username = serializers.CharField(label=_('用户名'))
    fullname = serializers.SerializerMethodField(method_name='get_fullname')
    telephone = serializers.CharField(label=_('电话'))
    company = serializers.CharField(label=_('公司/单位'))

    @staticmethod
    def get_fullname(obj):
        return obj.get_full_name()


class MeetingUserSerializer(serializers.Serializer):
    """会议预定用户"""
    id = serializers.CharField(label='Id', read_only=True)
    role = serializers.CharField(label=_('用户角色'))
    status = serializers.CharField(label=_('用户状态'))
    creation_time = serializers.DateTimeField(label=_('创建时间'))
    update_time = serializers.DateTimeField(label=_('修改时间'))
    user = UserSerializer(allow_null=True)


class MeetingUserUpdateSerializer(serializers.Serializer):
    """会议预定用户信息修改"""
    telephone = serializers.CharField(label=_('电话'), max_length=32)
    company = serializers.CharField(label=_('公司、单位'), max_length=254)
    first_name = serializers.CharField(label=_('名'), max_length=150)
    last_name = serializers.CharField(label=_('姓'), max_length=150)


class TermsConditionsSerializer(serializers.Serializer):
    """会议预定须知"""
    content = serializers.CharField(label='会议预定须知内容', required=True)


class ConfigsReserveTimesUpdateSerializer(serializers.Serializer):
    reserve_room_advance_minutes = serializers.IntegerField(
        label=_('提前（预定开始时间）多少分钟预留会议室资源'), min_value=15, max_value=180,
        allow_null=True, required=False, default=None)
    meeting_after_reserve_minutes = serializers.IntegerField(
        label=_('会议预定结束时间后，会议室资源多预留多少分钟'), min_value=0, max_value=180,
        allow_null=True, required=False, default=None)
    default_advance_notice_minutes = serializers.IntegerField(
        label=_('默认提前多少分钟发送会议提醒通知'), min_value=15, max_value=60*24*3,
        allow_null=True, required=False, default=None)


class ContentSerializer(serializers.Serializer):
    content = serializers.CharField(label='内容', required=True)


class NoShowSerializer(serializers.Serializer):
    id = serializers.CharField(label='Id', read_only=True)
    username = serializers.CharField(label=_('违约人账号'))
    user_nickname = serializers.CharField(label=_('违约人昵称'))
    creation_time = serializers.DateTimeField(label=_('违约时间'))
    valid_status = serializers.CharField(label=_('有效状态'))
    # remarks = serializers.CharField(label=_('备注'))
    email_notice_id = serializers.CharField(label=_('邮件通知记录'))
    reservation = BaseMeetingReservationSerializer(allow_null=True)


class NoShowWhitelistSerializer(serializers.Serializer):
    id = serializers.CharField(label='Id', read_only=True)

    username = serializers.CharField(label=_('用户账号'), max_length=128, required=True)
    user_nickname = serializers.CharField(label=_('姓名'), max_length=64, allow_blank=True, default='')
    remarks = serializers.CharField(label=_('备注'), max_length=254, allow_blank=True, default='')

    creator = serializers.CharField(label=_('创建人'), max_length=128, read_only=True)
    creation_time = serializers.DateTimeField(label=_('创建时间'), read_only=True)
    update_time = serializers.DateTimeField(label=_('修改时间'), read_only=True)

